/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  VerticalTextView.java   
 * @Package com.jiyi.widget   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-5 下午2:03:59   
 * @version V1.0     
 */
package com.jiyi.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swift.maintenance.R;

/**
 * @ClassName: VerticalTextView
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-5 下午2:03:59
 * 
 */
public class VerticalTextView extends LinearLayout {

	//判断是否有连续的数字或者非中文字符
	private String num = "";
	private Context context;
	//内容
	private String text;
	//默认字体大小
	private static final int DEFAULT_TEXT_SIZE = 14;
	//默认字体颜色
	private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
	//字体颜色
	private int color = DEFAULT_TEXT_COLOR;
	//字体大小
	private int size = DEFAULT_TEXT_SIZE;
	
	public VerticalTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		setText(text);
	}
	
	public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOrientation(VERTICAL);
		this.context = context;
		
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalTextView, defStyle, 0);
        size = a.getDimensionPixelSize(R.styleable.VerticalTextView_size, DEFAULT_TEXT_SIZE);
        color = a.getColor(R.styleable.VerticalTextView_color, DEFAULT_TEXT_COLOR);
        text = a.getString(R.styleable.VerticalTextView_content);
        a.recycle();
        setText(text);
    }

	
	public VerticalTextView(Context context) {
		super(context);
		setOrientation(VERTICAL);
		this.context = context;
	}

	public void setText(String text) {
		this.text = text;
		addText();
	}

	private void addText() {
		removeAllViews();
		num = "";
		if (text != null) {
			char[] chara = text.toCharArray();
			for (int i = 0; i < chara.length; i++) {
				System.out.println("什么情况------" + text);
				if (!isChinese(chara[i])) {
					num += chara[i];
					continue;
				}
				if (!TextUtils.isEmpty(num)) {
					crateText(num);
					num = "";
				}
				crateText(text.substring(i, i + 1));
			}
		}

	}

	public void setTextColor(int color) {
		this.color = color;
	}

	public void setTextSize(int size) {
		this.size = size;
	}
	
	/**   
	 * @author: HYM 
	 * @Title: crateText   
	 * @Description: TODO(创建一个TextView对象)   
	 * @param word void 
	 * @date: 2017-6-5
	 */  
	private void crateText(String word)
	{
		TextView oneText = new TextView(context);
		oneText.setTextColor(color);

		oneText.setText(word);
		
		if (size > 0) {
			oneText.setTextSize(size);
		}
		
		LayoutParams lpLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lpLayoutParams.gravity = Gravity.CENTER;
		oneText.setLayoutParams(lpLayoutParams);
		addView(oneText);
	}

	// 根据Unicode编码完美的判断中文汉字和符号
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}
}
